﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LinkCard.CustomerView.Common
{
    public class Pager
    {
        public Pager() { }

        /// <summary>
        /// <para>-- Tạo div phân trang dạng --</para>
        ///<para>&lt;div class='paging'&gt;</para>
        ///<para>    &lt;span id='ctl00_ctph_dtPgArticle'&gt;</para>
        ///<para>        &lt;a href='?Page=1'&gt;1&lt;/a&gt;&lt;span&gt;2&lt;/span&gt;</para>
        ///<para>        &lt;a href='?Page=3'&gt;3&lt;/a&gt;</para>
        ///<para>        &lt;a href='?Page=4'&gt;4&lt;/a&gt;</para>
        ///<para>        &lt;a href='?Page=5'&gt;5&lt;/a&gt;</para>
        ///<para>        &lt;a href='?Page=6'&gt;...&lt;/a&gt;</para>
        ///<para>    &lt;/span&gt;</para>
        ///<para> &lt;/div&gt;</para>
        /// </summary>
        /// <returns></returns>
        public static string Create(int curentPage, int totalPage)
        {
           /*
            <div class="paging">
                <span id="ctl00_ctph_dtPgArticle">
                    <a href="?Page=1">1</a>&nbsp;<span>2</span>&nbsp;
                    <a href="?Page=3">3</a>&nbsp;
                    <a href="?Page=4">4</a>&nbsp;
                    <a href="?Page=5">5</a>&nbsp;&nbsp;
                    <a href="?Page=6">...</a>&nbsp;
                </span>
            </div>
            */
             
            string html;
            if (totalPage <= 1)
                return "";

            if (totalPage <= 5)
            {
                html = @"<div class='paging'>
                            <span id='ctl00_ctph_dtPgArticle'>";
                for (int i = 1; i <= totalPage; i++)
                {
                    if (curentPage == i)
                        html += "<span>" + i + "</span>&nbsp;";
                    else
                        html += "<a href='?Page=" + i + "'>" + i + "</a>&nbsp;";
                }

                html += @"        </span>
                        </div>";
            }
            else 
            {
                html = @"<div class='paging'>
                            <span id='ctl00_ctph_dtPgArticle'>";
                for (int i = 1; i <= 5; i++)
                {
                    if (curentPage == i)
                        html += "<span>" + i + "</span>&nbsp;";
                    else
                        html += "<a href='?Page=" + i + "'>" + i + "</a>&nbsp;";
                }

                html+=@"<a href='?Page=6'>...</a>&nbsp;";

                html += @"        </span>
                        </div>";            
            }

      
            return html;
        }


        /// <summary>
        /// <para>-- Tạo div phân trang dạng --</para>
        ///<para>&lt;div class='paging'&gt;</para>
        ///<para>    &lt;span id='ctl00_ctph_dtPgArticle'&gt;</para>
        ///<para>        &lt;a href='?Page=1'&gt;1&lt;/a&gt;&lt;span&gt;2&lt;/span&gt;</para>
        ///<para>        &lt;a href='?Page=3'&gt;3&lt;/a&gt;</para>
        ///<para>        &lt;a href='?Page=4'&gt;4&lt;/a&gt;</para>
        ///<para>        &lt;a href='?Page=5'&gt;5&lt;/a&gt;</para>
        ///<para>        &lt;a href='?Page=6'&gt;...&lt;/a&gt;</para>
        ///<para>    &lt;/span&gt;</para>
        ///<para> &lt;/div&gt;</para>
        /// </summary>
        /// <returns></returns>
        public static string Create(int curentPage, int totalPage, List<KeyValuePair<string,object>> parameters)
        {
            /*
             <div class="paging">
                 <span id="ctl00_ctph_dtPgArticle">
                     <a href="?Page=1">1</a>&nbsp;<span>2</span>&nbsp;
                     <a href="?Page=3">3</a>&nbsp;
                     <a href="?Page=4">4</a>&nbsp;
                     <a href="?Page=5">5</a>&nbsp;&nbsp;
                     <a href="?Page=6">...</a>&nbsp;
                 </span>
             </div>
             */

            string html;
            if (totalPage <= 1)
                return "";

            if (totalPage <= 5)
            {
                html = @"<div class='paging'>
                            <span id='ctl00_ctph_dtPgArticle'>";
                for (int i = 1; i <= totalPage; i++)
                {
                    if (curentPage == i)
                        html += "<span>" + i + "</span>&nbsp;";
                    else
                    {
                        html += "<a href='?Page=" + i;
                        if (parameters != null)
                        {
                            foreach (KeyValuePair<string, object> param in parameters)
                            {
                                html += "&" + param.Key + "=" + param.Value;
                            }
                        }
                        html += "'>" + i + "</a>&nbsp;";
                    }
                }

                html += @"        </span>
                        </div>";
            }
            else
            {
                html = @"<div class='paging'>
                            <span id='ctl00_ctph_dtPgArticle'>";
                for (int i = 1; i <= 5; i++)
                {
                    if (curentPage == i)
                        html += "<span>" + i + "</span>&nbsp;";
                    else
                    {
                        html += "<a href='?Page=" + i;
                        if (parameters != null)
                        {
                            foreach (KeyValuePair<string, object> param in parameters)
                            {
                                html += "&" + param.Key + "=" + param.Value;
                            }
                        }
                        html += "'>" + i + "</a>&nbsp;";
                    }
                }

                html += @"<a href='?Page=6'>...</a>&nbsp;";

                html += @"        </span>
                        </div>";
            }


            return html;
        }
    }
}